package org.csu.mypetstore.api.service.impl;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Account;
import org.csu.mypetstore.api.entity.AccountSignon;
import org.csu.mypetstore.api.entity.AccountProfile;
import org.csu.mypetstore.api.persistence.AccountMapper;
import org.csu.mypetstore.api.persistence.AccountProfileMapper;
import org.csu.mypetstore.api.persistence.AccountSignonMapper;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.DatatypeConverter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountProfileMapper accountProfileMapper;
    @Autowired
    private AccountSignonMapper accountSignonMapper;

    private AccountVO accountToAccountVO(Account account, AccountProfile profile, AccountSignon signon){
        AccountVO accountVO = new AccountVO();
        //设置来自account表的字段
        accountVO.setUsername( account.getUsername() );
        accountVO.setEmail( account.getEmail() );
        accountVO.setFirstName( account.getFirstName() );
        accountVO.setLastName( account.getLastName() );
        accountVO.setStatus( account.getStatus() );
        accountVO.setAddress1( account.getAddress1() );
        accountVO.setAddress2( account.getAddress2() );
        accountVO.setCity( account.getCity() );
        accountVO.setState( account.getState() );
        accountVO.setZip( account.getZip() );
        accountVO.setCountry( account.getCountry() );
        accountVO.setPhone( account.getPhone() );

        //设置来自profile表的字段
        accountVO.setFavouriteCategoryId( profile.getFavouriteCategoryId() );
        accountVO.setLanguagePreference( profile.getLanguagePreference() );
        accountVO.setListOption( profile.isListOption() );
        accountVO.setBannerOption( profile.isBannerOption() );

        //设置来自signon表的字段
        accountVO.setPassword( signon.getPassword() );

        return accountVO;
    }

    @Override
    public String getEncryptedPassword( String salt, String password ) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance( "MD5" );
        //将用户名作为盐拼接到密码后方
        String originText = password.concat( salt );
        md.update( originText.getBytes() );
        byte[] digest = md.digest();
        String hashText = DatatypeConverter.printHexBinary(digest).toUpperCase();

        return hashText;
    }

    @Override
    public CommonResponse<List<AccountVO>> getAllAccountList() {
        List<Account> accountList = accountMapper.selectList( null );

        if ( accountList.size() == 0 ){
            return CommonResponse.createForSuccessMessage( "No account in database" );
        }else{
            Account account;
            AccountProfile profile;
            AccountSignon signon;
            AccountVO accountVO;
            List<AccountVO> accountVOList = new ArrayList<>();
            for(int i = 0; i < accountList.size(); i ++ ){
                account = accountList.get( i );
                profile = accountProfileMapper.selectById( account.getUsername() );
                signon = accountSignonMapper.selectById( account.getUsername() );
                accountVO = accountToAccountVO( account, profile, signon );
                accountVOList.add( accountVO );
            }
            return CommonResponse.createForSuccess(accountVOList);

        }
    }

    @Override
    public CommonResponse<Account> getAccount( String username ) {
        Account account = accountMapper.selectById( username );
        if(account == null){
            return CommonResponse.createForSuccessMessage( "No such account with the username given" );
        }
        return CommonResponse.createForSuccess(account);
    }

    @Override
    public CommonResponse<AccountVO> getAccount( String username, String password ) throws NoSuchAlgorithmException {
        //先查看是否有该用户名的用户
        Account account = accountMapper.selectById( username );
        if(account == null){
            return CommonResponse.createForSuccessMessage( "No such account with the username given" );
        }
        AccountSignon signon = accountSignonMapper.selectById( username );
        String pswEncrypted = getEncryptedPassword( username, password );
        //若密码错误
        if( !pswEncrypted.equals( signon.getPassword() )){
            return CommonResponse.createForSuccessMessage( "Wrong password" );
        }
        AccountProfile profile = accountProfileMapper.selectById( username );

        AccountVO accountVO = accountToAccountVO( account, profile, signon );
        return CommonResponse.createForSuccess(accountVO);
    }

    @Override
    public void insertAccount( Account account ) {

    }

    @Override
    public void removeAccount( Account account ) {

    }

    @Override
    public void updateAccount( Account account ) {

    }
}
